package com.framework.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.framework.dao.sys.SysTemplateDao;
import com.framework.entity.sys.SysTemplate;
import com.framework.exception.RRException;
import com.framework.page.PageUtils;
import com.framework.page.Query;
import com.framework.service.SysTemplateService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * @author wq
 */
@Service("sysTemplateService")
public class SysTemplateServiceImpl extends ServiceImpl<SysTemplateDao,SysTemplate> implements SysTemplateService {



    @Override
    public PageUtils queryTemplatePage(Map<String, Object> params) {
        Long deptId=0L;
        if(params.get("deptId")!=null){

            deptId= Long.valueOf(params.get("deptId").toString());
        }
        Integer type= 0;
        if(params.get("type")!=null){

            type= Integer.valueOf(params.get("type").toString());
        }
        Integer inOut= 0;
        if(params.get("inOut")!=null){

            inOut= Integer.valueOf(params.get("inOut").toString());
        }
        Page<SysTemplate> page = selectPage(new Query<SysTemplate>(params).getPage(), new EntityWrapper<SysTemplate>()
            .eq(deptId.intValue()!=0,"dept_id",deptId)
                .eq(type.intValue()!=0,"type",type)
                .eq(inOut.intValue()!=0,"in_out",inOut)

        );
        return new PageUtils(page);
    }

    @Override
    public void save(SysTemplate sysTemplate) {
        sysTemplate.setCreateDate(new Date());
        baseMapper.insert(sysTemplate);
    }

    @Override
    public Integer selectTemplateId(Long deptId) {
        SysTemplate t=new SysTemplate();
        t.setDeptId(deptId);
        t.setType(2);
        SysTemplate tem = baseMapper.selectOne(t);
        return tem.getId();
    }

    @Override
    public SysTemplate queryByTypeAndDeptId(Integer templateType, Long deptId) {
        return selectOne(new EntityWrapper<SysTemplate>().eq("type", templateType).eq("dept_id", deptId));
    }
}
